package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.BaseDAO;
import DTO.Employee;
import serviceImpl.VipDAO;
import util.UtilMD5;

@WebServlet(name = "e_login",urlPatterns = "/e_login")
public class VipLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public VipLoginServlet() {
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = null;
        flag = request.getParameter("flag");
        switch(flag){
            case "login":
                login(request,response);
                break;

            case "clear":
                clear(request,response);
                break;
        }
    }

    protected static void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        request.getSession().invalidate();
        response.sendRedirect("login.jsp");
    }


    protected static void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String username = request.getParameter("username").trim();
        String password = request.getParameter("password").trim();
        if(username=="" || password==""){
            request.setAttribute("error", "用户名和密码不能为空");
            request.getRequestDispatcher("login1.jsp").forward(request, response);
            return;
        }else{
            //创建一个管理员对象，用于存值
            Employee emp = new Employee();
            emp.setName(username);
            emp.setPassword(password);
            BaseDAO<Employee> ad = new VipDAO();
            try {
                emp = ad.findByPropert(emp);
                if(emp != null){
                    if(emp.getPassword().equals(UtilMD5.MD5U(password))){
                        String autologin = request.getParameter("autologin");
                        if("autologin".equals(autologin)){
                            Cookie cname = new Cookie("e_username",username);
                            Cookie cpassword = new Cookie("e_password",password);
                            //设置Cookie有效时间
                            cname.setMaxAge(60*60*24*7);
                            cpassword.setMaxAge(60*60*24*7);
                            response.addCookie(cname);
                            response.addCookie(cpassword);
                            System.out.println("进入页面成功" + emp);
                            request.getSession().setAttribute("emp", emp);
                            response.sendRedirect("e_loading.html");
                        }else{
                        	System.out.println("进入页面成功" + emp);
                            request.getSession().setAttribute("emp", emp);
                            response.sendRedirect("e_loading.html");
                        }
                    }
                    else{
                        request.setAttribute("error", "用户名或者密码错误");
                        request.getRequestDispatcher("login1.jsp").forward(request, response);
                    }
                }
                else{
                    request.setAttribute("error", "用户名或者密码错误");
                    request.getRequestDispatcher("login1.jsp").forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }




    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
    }

}
